﻿using IMES.Common.Domain.DurianModel;
using IMES.Common.Domain.GoldIngotModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GoldIngot.API.Application.Commands.CompletionOrderCommand
{
    public class CompletionOrderSelectHandler : IRequestHandler<CompletionOrderSelectCommand, Result>
    {
        private readonly IBaseRepositorys<CompletionOrder> orderRep;
        private readonly IBaseRepositorys<ProcessManagement> processRep;

        public CompletionOrderSelectHandler(IBaseRepositorys<CompletionOrder> orderRep, IBaseRepositorys<ProcessManagement> processRep)
        {
            this.orderRep = orderRep;
            this.processRep = processRep;
        }

        public async Task<Result> Handle(CompletionOrderSelectCommand request, CancellationToken cancellationToken)
        {
            var quest = new Result() { Code = (int)CodeEnum.success };

            var list = (from a in orderRep.GetAll()
                        join b in processRep.GetAll() on a.ProcessID equals b.ProcessID
                        select new OrederManageDTO()
                        {
                            CompletionId = a.CompletionId,
                            ProcessID = b.ProcessID,
                            ProcessName = b.ProcessName,
                            ProcessNumber = b.ProcessNumber,
                            WorkSheetId = a.WorkSheetId,
                            CompletionOrderStatus = a.CompletionOrderStatus,
                            CompletionOrderDate = a.CompletionOrderDate,
                            CreateDate = a.CreateDate,
                            CreateName = a.CreateName,
                            UpdateDate = a.UpdateDate,
                            UpdateName = a.UpdateName,
                        });
            if (!string.IsNullOrEmpty(request.ProcessName))
            {
                list = list.Where(a => a.ProcessName==request.ProcessName);
            }
            if (!string.IsNullOrEmpty(request.WorkSheetNumber))
            {
                list = list.Where(a => a.WorkSheetNumber == request.WorkSheetNumber);
            }
            if (request.ProcessNumber != 0)
            {
                list = list.Where(a => a.ProcessNumber == request.ProcessNumber);
            }
            quest.Data = list;
            return quest;
        }
    }
}
